import { EventsModule } from './adapter/events.module';
import { UserEntity } from './entities/sys/user.entity';
import { UserRoleEntity } from './entities/sys/user_role.entity';
import { Module, Logger } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from 'nestjs-config';
import { resolve, join } from 'path';
import { AdminModule } from './controllers/admin/admin.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { FrontModule } from './controllers/front/front.module';
import { LoggingInterceptor } from '@src/shared/interceptor/logging/logging.interceptor';
import { CartGateway } from '@src/gateway/cart/cart.gateway';
import { ScheduleModule } from '@nestjs/schedule';

const CWD = process.cwd();
const ENV = process.env.NODE_ENV;
Logger.log(`当前环境==>${CWD}`);
Logger.log(`当前模式==>${ENV}`);

@Module({
  imports: [
    // 配置加载配置文件
    ConfigModule.load(resolve(__dirname, 'config', '**/!(*.d).{ts,js}'), {
      modifyConfigName: name => name.replace('.config', ''),
      path: resolve(process.cwd(), !ENV ? '.env' : `${ENV}.env`),
    }),
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => {
        const entity = [resolve(__dirname, 'entities/**/*.entity.{ts,js}')];
        const orm = Object.assign({}, config.get('database'), { entities: entity });
        Logger.log(`数据库配置==》${JSON.stringify(orm)}`);
        return orm
      },
      inject: [ConfigService],
    }),
    FrontModule,
    AdminModule,
    ScheduleModule.forRoot(),
    EventsModule
  ],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor,
    },
  ],
  controllers: [],
})
export class AppModule {
}
